import { domain } from '@/stores/useApp.js'
import htmlParser from '@/common/html-parser'

export default function parseHtml (html) {
	let imgs = [];
	let nodes = htmlParser(html);
	return parseImgs(nodes, imgs);
}

function parseImgs(nodes, imgs) {
	let needSetColor = ['p', 'span', 'text'];
	nodes.forEach(node => {
		if (!node.attrs) {
			node.attrs = {
				"style": ""
			};
		}

		if (node.name === 'img' ) {
			node.attrs.style = `width: 100%;height: auto;`
			let src = `${domain}${node.attrs.src}`;
			node.attrs.src = src;
			imgs.push(src);
		}

		if (needSetColor.indexOf(node.name) >= 0) {			
			if (node.attrs.style) { 
				node.attrs.style = `${node.attrs.style};color:#fff;`
			} else {
				node.attrs["style"] = "color: #fff";
			}
		}

		if (Array.isArray(node.children)) {
			parseImgs(node.children, imgs)
		}
	})
	return { nodes, imgs }
}